package com.bw.gmallpublisher.controller;

import com.bw.gmallpublisher.service.GmvService;
import com.bw.gmallpublisher.service.UvService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController    //controller   responseBody
@RequestMapping("/api/sugar")
public class SugarController {
    @Autowired
    GmvService   gmvservice;
    @Autowired
    UvService uvService;
    @RequestMapping("/getGMV")
    public String   getGMV(@RequestParam(value = "date",defaultValue = "0") int  date){
        if(date==0){
            //创建获取当前时间的方法
             date= getDate();
        }
        //数据格式以第三方图形静态数据为主
        System.out.println(date);



        return  "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": "+gmvservice.getGmv(date)+"\n" +
                "}";
    }
    private int getDate() {
        long ts = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return  Integer.parseInt(sdf.format(ts));
    }

   @RequestMapping("/getUvByCh")
    public  String  getUvByCh(@RequestParam(value = "date",defaultValue = "0") int  date){
        if(date==0){
            //创建获取当前时间的方法
            date= getDate();
        }
        //数据格式以第三方图形静态数据为主
        System.out.println(date);
        Map map = uvService.selectUVByCH(date);
        //获取key
        Set set = map.keySet();
        //获取value
        Collection values = map.values();
       System.out.println(values);
        String join1 = StringUtils.join(set, "\",\"");
        String join2 = StringUtils.join(values, ",");

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": [\n\"" +
                join1+
                "\"    ],\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"访问渠道\",\n" +
                "        \"data\": [\n" +
                join2+
                "        ]\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";


    }



}
